export function guid() {
  function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return (
    S4() +
    S4() +
    "-" +
    S4() +
    "-" +
    S4() +
    "-" +
    S4() +
    "-" +
    S4() +
    S4() +
    S4()
  );
}
export function toColorHex(color: string) {
  const reg = /^(rgb|RGB)/;
  if (reg.test(color)) {
    let strHex = "#";
    const colorArr = color.replace(/(?:\(|\)|rgb|RGB)*/g, "").split(",");
    for (let i = 0; i < colorArr.length; i++) {
      let hex = Number(colorArr[i]).toString(16);
      if (hex.length === 1) {
        hex = "0" + hex;
      }
      strHex += hex;
    }
    return strHex;
  }
  return color;
}
export function toColorRgb(color: string) {
  const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  if (reg.test(color)) {
    if (color.length === 4) {
      let colorNew = "#";
      for (let i = 1; i < 4; i++) {
        colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
      }
      color = colorNew;
    }
    let colorChange = [];
    for (let i = 1; i < 7; i += 2) {
      colorChange.push(parseInt("0x" + color.slice(i, i + 2)));
    }
    return "rgb(" + colorChange.join(",") + ")";
  }
  return color;
}
// 函数防抖
export function debounce(func: Function, delay: number, context: any) {
  let timer: string | number | NodeJS.Timeout | undefined;
  return (...args: any[]) => {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      func.call(context, ...args);
    }, delay);
  };
}
// 函数节流
export function throttle(func: Function, delay: number, context: any) {
  let time1 = 0;
  return (...args: any[]) => {
    const time2 = Date.now();
    const interval = time2 - time1;
    if (interval < delay) {
      return;
    } else {
      time1 = time2;
      func.call(context, ...args);
    }
  };
}
